package com.ctrip.cap.amqp;

import java.util.Properties;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

public class Consumer {
	/**
	 * Get Message From spec queue
	 * @param queueName
	 * @return Message
	 */
	public static String GetMessage(String queueName) {

		try {
			CachingConnectionFactory connectionFactory = new CachingConnectionFactory("csm-haddop03.dev.sh.ctripcorp.com");
			connectionFactory.setUsername("admin");
			connectionFactory.setPassword("123456");

			RabbitTemplate template = new RabbitTemplate(connectionFactory);
			template.setRoutingKey(queueName);
			template.setQueue(queueName);

			return template.receiveAndConvert().toString();
		} catch (Exception e) {
			return "";
		}
	}
	
	public static int GetCount(String queueName){
		try {
			CachingConnectionFactory connectionFactory = new CachingConnectionFactory("csm-haddop03.dev.sh.ctripcorp.com");
			connectionFactory.setUsername("admin");
			connectionFactory.setPassword("123456");
			RabbitAdmin admin = new RabbitAdmin(connectionFactory);
			Properties queueProp = admin.getQueueProperties(queueName);
			
			return (int)queueProp.get("QUEUE_MESSAGE_COUNT");
			
		} catch (Exception e) {
			return -1;
		}
	}
}